package edu.uj.util;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * utility class for storing observers observing an objects.
 * @author Piotrek
 */
public class ObserverList implements Observed, Serializable {
    private static final long serialVersionUID = -6608593734042609453L;
    
    private Set<Observer> observers;

    public ObserverList() {
        observers = new HashSet<Observer>();
    }

    public void subscribe(Observer o) {
        observers.add(o);
    }

    public void unsubscribe(Observer o) {
        observers.remove(o);
    }
    
    /**
     * removes all observers from the list
     */
    public void clear() {
       observers.clear();
    }

    public void notifyAllSubscribers(int eventCode) {
        for(Observer o : observers)
            o.notifyFromObserved(eventCode);
    }
}
